package br.eng.eliseu.gwt.gestaoWeb.server.command.generico;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.persistence.Query;

import br.eng.eliseu.gwt.gestaoWeb.server.command.BaseCommand;

import com.google.inject.persist.Transactional;

public class ExecuteUpdateCommand extends BaseCommand<Void> /*implements Callable<Void>*/ {

	private static final long serialVersionUID = -5671574277758231289L;

	private Map<String, Object> params = new HashMap<String, Object>();
	private String NamedQuery;
	
//	@Inject EntityManager entityManager;
	
	@SuppressWarnings("rawtypes")
	@Transactional
	public Void call() {
		Query query = getEntityManager().createNamedQuery(NamedQuery);
		if (params!=null){
			for (Iterator iterator = params.keySet().iterator(); iterator.hasNext();) {
				String parametro = (String) iterator.next();
				query.setParameter(parametro, params.get(parametro));
			}
		}
		query.executeUpdate();
		return null;
	}

	public Map<String, Object> getParams() {
		return params;
	}

	public void setParams(Map<String, Object> params) {
		this.params = params;
	}

	public void addParameter(String key, Object valor){
		params.put(key, valor);
	}
	
	public String getNamedQuery() {
		return NamedQuery;
	}

	public void setNamedQuery(String queryName) {
		this.NamedQuery = queryName;
	}

}
